"use strict";

/**
 * 图片滑动，选项卡切换组件
 * 依赖：jQuery
 * 张书振
 * 2015-11-20
 */
;(function ($) {
	"use strict";

	$.houseSlider = function (options) {
		var opts = {
			num: 0, //切换卡片数量
			speed: 300, //动画持续时间
			easeing: "swing", //缓动公式
			animateType: 2, //动画类型，如果是一个函数则为自定义切换类型，传入参数为currIdx
			autoPlay: 0, //0不进行自动播放
			$leftBtn: null,
			$rightBtn: null,
			$ctrList: null, //索引列表
			$cont: null, //内容区父容器
			$contList: null, //内容区列表
			isLoop: false, //是否循环播放
			callback: function callback(currIdx, opts) {} //切换后回调
		};
		opts = $.extend(opts, options || {});
		opts.$contList = opts.$cont.children();
		opts.num = opts.$contList.size();

		//无内容直接退出
		if (opts.$cont === null || opts.$cont.size() === 0) {
			return;
		}
		//如果循环播放就克隆第一个页放到最后
		// if(opts.isLoop) {
		// 	opts.$contList.first().clone().appendTo(opts.$cont);
		// }
		var currIdx = 0;
		if (opts.$leftBtn) {
			opts.$leftBtn.click(function () {
				if (currIdx === 0) {
					currIdx = opts.num - 1;
				} else {
					currIdx--;
				}
				_gotoIdx(opts, currIdx, "LEFT_BUTTON");
			});
			opts.$rightBtn.click(function () {
				if (currIdx + 1 === opts.num) {
					currIdx = 0;
				} else {
					currIdx++;
				}
				_gotoIdx(opts, currIdx, "RIGHT_BUTTON");
			});
		}
		if (opts.$ctrList) {
			opts.$ctrList.mouseenter(function () {
				var $t = $(this);
				var idx = opts.$ctrList.index($t);
				if (idx === currIdx) {
					return;
				}
				currIdx = idx;
				_gotoIdx(opts, currIdx);
			});
		}

		var isMouseEnter = false;
		if (opts.autoPlay) {
			setInterval(function () {
				if (!isMouseEnter) {
					if (currIdx + 1 === opts.num) {
						currIdx = 0;
					} else {
						currIdx++;
					}
					_gotoIdx(opts, currIdx, "RIGHT_BUTTON");
				}
			}, opts.autoPlay);

			// opts.$cont.mouseenter(function() {
			// 	isMouseEnter = true;
			// }).mouseleave(function() {
			// 	isMouseEnter = false;
			// });
			if (opts.$leftBtn) {
				opts.$leftBtn.mouseenter(function () {
					isMouseEnter = true;
				}).mouseleave(function () {
					isMouseEnter = false;
				});
				opts.$rightBtn.mouseenter(function () {
					isMouseEnter = true;
				}).mouseleave(function () {
					isMouseEnter = false;
				});
			}
		}

		return {
			goTo: function goTo(idx) {
				currIdx = idx;
				_gotoIdx(opts, idx);
			}
		};
	};
	$.houseSlider.HOUSE_SLIDER_NONE = 0; //没有效果
	$.houseSlider.HOUSE_SLIDER_SLIDE_V = 1; //垂直滑动
	$.houseSlider.HOUSE_SLIDER_SLIDE_H = 2; //水平滑动
	$.houseSlider.HOUSE_SLIDER_FADEIN = 3; //淡入淡出

	// 显示指定索引内容
	function _gotoIdx(opts, currIdx, button) {
		if (typeof opts.animateType === "function") {
			//自定义切换效果
			opts.animateType(currIdx);
		} else if (opts.animateType === $.houseSlider.HOUSE_SLIDER_NONE) {
			//没有效果
			opts.$contList.hide();
			opts.$contList.eq(currIdx).show();
		} else if (opts.animateType === $.houseSlider.HOUSE_SLIDER_SLIDE_V) {
			//垂直滑动
			opts.$cont.stop(true, true).animate({
				marginTop: -(currIdx * opts.$contList.height())
			}, opts.speed, opts.easeing);
		} else if (opts.animateType === $.houseSlider.HOUSE_SLIDER_SLIDE_H) {
			//水平滑动
			if (opts.isLoop && button === "RIGHT_BUTTON" && currIdx === 0) {
				opts.$cont.stop(true, true).animate({
					marginLeft: -(opts.num * opts.$contList.width())
				}, opts.speed, opts.easeing, function () {
					opts.$cont.css({ "marginLeft": 0 });
				});
			} else if (opts.isLoop && button === "LEFT_BUTTON" && currIdx + 1 === opts.num) {
				opts.$cont.stop(true, true).css({ "marginLeft": -(opts.num * opts.$contList.width()) });
				opts.$cont.animate({
					marginLeft: -(currIdx * opts.$contList.width())
				}, opts.speed, opts.easeing);
			} else {
				opts.$cont.stop(true, true).animate({
					marginLeft: -(currIdx * opts.$contList.width())
				}, opts.speed, opts.easeing);
			}
		} else if (opts.animateType === $.houseSlider.HOUSE_SLIDER_FADEIN) {
			//淡入模式
			opts.$contList.stop(true, true);
			opts.$contList.fadeOut(opts.speed);
			opts.$contList.eq(currIdx).fadeIn(opts.speed);
		}

		if (opts.$ctrList) {
			opts.$ctrList.removeClass("ne-active");
			opts.$ctrList.eq(currIdx).addClass("ne-active");
		}

		if (typeof opts.callback === "function") {
			opts.callback(currIdx, opts);
		}
	}
})(jQuery);